package com.passioneffect.widgets {
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import gs.TweenMax;	

	/**
	 * @author Passion Effect
	 */
	public class Pan {

		public var xPan : Number;
		private var target : DisplayObject;
		public var range : Rectangle;
		private var at : DisplayObjectContainer;
		public var originalY : Number;
		public var originalX : Number;
		public var yPan : Number;

		public function Pan(t:DisplayObject,range:Rectangle,at:DisplayObjectContainer,xPan:Number=0,yPan:Number=NaN) {
			this.target=t;
			this.xPan=xPan;
			this.range=range;
			this.at=at;
			this.originalX=t.x;
			this.originalY=t.y;
			this.yPan=yPan;
		}
		
		public function open():void{
			target.stage.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
		}
		
		public function close():void{
			target.stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
			TweenMax.killTweensOf(target);
		}

		private function moveHandler(event : MouseEvent) : void {
			if(range.contains(at.mouseX, at.mouseY)){
				var xPercent:Number=(at.mouseX-range.x)/range.width;
				var yPercent:Number=(at.mouseY-range.y)/range.height;
				
				var offsetx:Number=xPercent*xPan*2-xPan;
				var offsety:Number=yPercent*yPan*2-yPan;
				
				if(!isNaN(xPan))TweenMax.to(target,0.3,{x:originalX-offsetx});
				if(!isNaN(yPan))TweenMax.to(target,0.3,{y:originalY-offsety});
				
			}else{
				//if(!isNaN(xPan))TweenMax.to(target,0.3,{x:originalX});
				//if(!isNaN(yPan))TweenMax.to(target,0.3,{y:originalY});
			}
			
		}
	}
}